﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class Views_EmployeesSalaries : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StorecbxAddManaProvince_LoadData();
            StoreEmployeeSalary_LoadData();
        }
    }

    protected void StorecbxAddManaProvince_LoadData()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                StorecbxAddManaProvince.DataSource = db.Provinces.ToList<Province>();
                StorecbxAddManaProvince.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    public void StoreEmployeeSalary_LoadData()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                if (cbxUpdateForEmpProfiles.Value != null)
                {
                    string selectedValue = cbxUpdateForEmpProfiles.Value.ToString();
                    if (!cbxUpdateForEmpProfiles.Value.ToString().Equals(""))
                    {
                        int employeeID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());

                        if (employeeID > 0)
                        {
                            ToolbarShowAdd.Disabled = false;
                        }
                        else
                            ToolbarShowAdd.Disabled = true;

                        var result = db.Proc_EmployeesSalaryLevels_EmpID(employeeID).ToList<Proc_EmployeesSalaryLevels_EmpID_Result>();

                        StoreEmpSalary.DataSource = result;
                        StoreEmpSalary.DataBind();

                        return;
                    }
                }
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    public void RowSelected(object sender, DirectEventArgs e)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                RowSelectionModel rsm = GridPanelEmpSalary.SelectionModel.Primary as RowSelectionModel;

                int empSalaryID = Int32.Parse(rsm.SelectedRow.RecordID);
                EmployeesSalaryLevel empSalary = db.EmployeesSalaryLevels.Single(es => es.EmpSalaryID == empSalaryID);

                lblCodeEmpChangeWork.Text = empSalary.CodeEmpChangeWork;
                lblNameOfSalary.Text = empSalary.SalaryLevel.NameOfSalary;
                lblCode.Text = empSalary.SalaryLevel.Code;
                lblLevelSalary.Text = empSalary.SalaryLevel.LevelSalary;
                lblNumberSalary.Text = empSalary.SalaryLevel.NumberSalary;
                lblAllowancesPostion.Text = empSalary.AllowancesPostion.ToString();
                lblAllowancesOther.Text = empSalary.AllowancesOther.ToString();
                lblStartOfSalary.Text = DateTime.Parse(empSalary.StartOfSalary.ToString()).ToString("dd/MM/yyyy");
                lblEndOfWork.Text = DateTime.Parse(empSalary.EndOfWork.ToString()).ToString("dd/MM/yyyy");
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void cbxAddManaProvince_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int provID = Int32.Parse(cbxAddManaProvince.Value.ToString());
                Province provSelected = db.Provinces.Single(p => p.ProvID == provID);

                List<District> list = provSelected.Districts.ToList<District>();
                StoreViewDistrictByProvinces.DataSource = list;
                StoreViewDistrictByProvinces.DataBind();


                StoreAddManaCommune.DataSource = new List<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
                Resert_GirdView();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDistrict_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int distID = Int32.Parse(cbxAddManaDistrict.Value.ToString());

                District dist = db.Districts.Single(p => p.DistID == distID);

                StoreAddManaCommune.DataSource = dist.Communes.ToList<Commune>();
                StoreAddManaCommune.DataBind();

                StoreAddManaOrganization.DataSource = new List<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
                Resert_GirdView();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaCommune_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int comID = Int32.Parse(cbxAddManaCommune.Value.ToString());

                Commune dist = db.Communes.Single(c => c.ComID == comID);

                StoreAddManaOrganization.DataSource = dist.Organizations.ToList<Organization>();
                StoreAddManaOrganization.DataBind();

                StoreAddManaDepartment.DataSource = new List<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();
                Resert_GirdView();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaOrganization_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int orgID = Int32.Parse(cbxAddManaOrganization.Value.ToString());

                Organization org = db.Organizations.Single(o => o.OrgID == orgID);

                StoreAddManaDepartment.DataSource = org.Departments.ToList<Department>();
                StoreAddManaDepartment.DataBind();

                StorecbxUpdateForEmpProfiles.DataSource = new List<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();

                Resert_GirdView();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    [DirectMethod]
    public void cbxAddManaDepartment_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int depID = Int32.Parse(cbxAddManaDepartment.Value.ToString());

                Department dep = db.Departments.Single(o => o.DepID == depID);

                StorecbxUpdateForEmpProfiles.DataSource = dep.Employees.ToList<Employee>();
                StorecbxUpdateForEmpProfiles.DataBind();

                Resert_GirdView();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }
    }

    //Nhân viên
    [DirectMethod]
    public void cbxUpdateForEmpProfiles_SelectedChange()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int employeeID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());

                if (employeeID > 0)
                {
                    ToolbarShowAdd.Disabled = false;
                }
                else
                    ToolbarShowAdd.Disabled = true;

                var result = db.Proc_EmployeesSalaryLevels_EmpID(employeeID).ToList<Proc_EmployeesSalaryLevels_EmpID_Result>();

                StoreEmpSalary.DataSource = result;
                StoreEmpSalary.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    protected void StoreEmpSalary_ReadData(object sender, StoreReadDataEventArgs e)
    {
        StoreEmployeeSalary_LoadData();
    }

    protected void ToolbarShowAdd_DirectClick(object sender, DirectEventArgs e)
    {
        WindownAddEmpSalary.Hidden = false;
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                List<SalaryLevel> list = db.SalaryLevels.ToList<SalaryLevel>();
                StoreSalary.DataSource = list;
                StoreSalary.DataBind();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau");
        }

    }

    [DirectMethod]
    public void EmployeeSalary_Add()
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int empID = Int32.Parse(cbxUpdateForEmpProfiles.Value.ToString());
                int salaryID = Int32.Parse(cbxAddSalary.Value.ToString());
                string codeChange = TextAddCodeEmpChangeWork.Text.Trim();

                List<EmployeesSalaryLevel> listEmpSalary = db.EmployeesSalaryLevels.ToList<EmployeesSalaryLevel>();
                foreach (var item in listEmpSalary)
                {
                    if (item.CodeEmpChangeWork.ToLower().Equals(codeChange.ToLower()))
                    {
                        X.Msg.Alert("Cảnh báo", "Mã công tác đã được sử dụng hãy nhập mã khác").Show();
                        return;
                    }
                }

                EmployeesSalaryLevel es = new EmployeesSalaryLevel()
                {
                    EmpID = empID,
                    SaLevID = salaryID,
                    CodeEmpChangeWork = codeChange,
                    AllowancesPostion = Double.Parse(TextAddAllowancesPostion.Text.Trim()),
                    AllowancesOther = Double.Parse(TextAddAllowancesOther.Text.Trim()),
                    StartOfSalary = DateTime.Parse(TextAddStartOfSalary.Text),
                    EndOfWork = DateTime.Parse(TextAddEndOfWork.Text)
                };

                db.EmployeesSalaryLevels.Add(es);
                db.SaveChanges();
                StoreEmployeeSalary_LoadData();

                WindownAddEmpSalary.Hidden = true;
                X.Msg.Alert("Thông báo", "Thêm bậc lương cho nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau").Show();
        }
    }

    [DirectMethod]
    public void Update_Clicked(string sid)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int empSalaryID = Int32.Parse(sid);
                EmployeesSalaryLevel empSalary = db.EmployeesSalaryLevels.Single(es => es.EmpSalaryID == empSalaryID);

                TextUpdateCodeEmpChangeWork.Text = empSalary.CodeEmpChangeWork;

                StoreUpdateSalaryLevel.DataSource = db.SalaryLevels;
                StoreUpdateSalaryLevel.DataBind();
                SelectSalaryLevel.Value = empSalary.SaLevID;
                //SelectSalaryLevel.Disabled = true;


                TextUpdateAllowancesPostion.Text = empSalary.AllowancesPostion.ToString();
                TextUpdateAllowancesOther.Text = empSalary.AllowancesOther.ToString();
                TextUpdateStartOfSalary.Text = empSalary.StartOfSalary.ToString();
                TextUpdateEndOfWork.Text = empSalary.EndOfWork.ToString();

                TextEmpSalaryIDUpdate.Text = sid;
                WindowUpdateEmpSalary.Hidden = false;
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, xin bạn vui lòng quay lại sau").Show();
        }
    }

    [DirectMethod]
    public void EmployeesSalaryLevel_Update()
    {
        int esID = Int32.Parse(TextEmpSalaryIDUpdate.Text.Trim());
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int salaryID = Int32.Parse(SelectSalaryLevel.Value.ToString());

                string codeChange = TextUpdateCodeEmpChangeWork.Text.Trim();

                List<EmployeesSalaryLevel> listEmpSalary = db.EmployeesSalaryLevels.ToList<EmployeesSalaryLevel>();
                foreach (var item in listEmpSalary)
                {
                    if (item.CodeEmpChangeWork.ToLower().Equals(codeChange.ToLower()) && item.EmpSalaryID != esID)
                    {
                        X.Msg.Alert("Cảnh báo", "Mã công tác đã được sử dụng hãy nhập mã khác").Show();
                        return;
                    }
                }

                EmployeesSalaryLevel emps = db.EmployeesSalaryLevels.Single(em => em.EmpSalaryID == esID);

                emps.CodeEmpChangeWork = codeChange;
                emps.SaLevID = Int32.Parse(SelectSalaryLevel.Value.ToString());
                emps.AllowancesPostion = Double.Parse(TextUpdateAllowancesPostion.Text);
                emps.AllowancesOther = Double.Parse(TextUpdateAllowancesOther.Text);
                emps.StartOfSalary = DateTime.Parse(TextUpdateStartOfSalary.Text);
                emps.EndOfWork = DateTime.Parse(TextUpdateEndOfWork.Text);

                db.SaveChanges();
                StoreEmployeeSalary_LoadData();

                WindowUpdateEmpSalary.Hidden = true;
                X.Msg.Alert("Thông báo", "Cập nhật thông tin bậc lương cho nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Hệ thống thông báo", "Hệ thống đang bận, hãy thử lại sau").Show();
        }
    }

    [DirectMethod]
    public void Delete_clicked(string emslID)
    {
        X.Msg.Confirm("Thông báo ", String.Format("Bạn có muốn bậc lương này không?"),
                        new MessageBoxButtonsConfig()
                        {
                            Yes = new MessageBoxButtonConfig
                            {
                                Text = "Đóng",
                                Handler = "App.direct.EmployeeSalaryLeval_Delete(" + emslID + ");"
                            },
                            No = new MessageBoxButtonConfig
                            {
                                Text = "Hủy bỏ"
                            }
                        }).Show();
    }

    [DirectMethod]
    public void EmployeeSalaryLeval_Delete(string emslID)
    {
        try
        {
            using (ManagerEmployeesEntities db = new ManagerEmployeesEntities())
            {
                int frID = Int32.Parse(emslID);

                EmployeesSalaryLevel empSL = db.EmployeesSalaryLevels.Single(f => f.EmpSalaryID == frID);
                db.EmployeesSalaryLevels.Remove(empSL);

                db.SaveChanges();
                StoreEmployeeSalary_LoadData();
                ResetFormView();
                X.Msg.Alert("Thông báo", "Xóa thông tin bậc lương nhân viên thành công").Show();
            }
        }
        catch (Exception)
        {
            X.Msg.Alert("Thông báo", "Hệ thống đang bận, hãy quay lại sau").Show();
        }
    }

    public void ResetFormView()
    {
        lblCodeEmpChangeWork.Text = "";
        lblNameOfSalary.Text = "";
        lblCode.Text = "";
        lblLevelSalary.Text = "";
        lblNumberSalary.Text = "";
        lblAllowancesPostion.Text = "";
        lblAllowancesOther.Text = "";
        lblStartOfSalary.Text = "";
        lblEndOfWork.Text = "";
    }

    public void Resert_GirdView()
    {
        StoreEmpSalary.DataSource = new List<Object>();
        StoreEmpSalary.DataBind();
        ResetFormView();
    }
}